package com.neu.view;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;

import com.neu.pojo.Model;
import com.neu.pojo.Patient;
import com.neu.pojo.Problem;
import com.neu.pojo.Result;
import com.neu.pojo.Worker;
import com.neu.service.ImodelService;
import com.neu.service.impl.ModelService;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.Date;

public class AnswerUI extends JFrame {

	private JPanel contentPane;
	private ImodelService modelService = ModelService.getInstance();
	ButtonGroup[] group;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AnswerUI frame = new AnswerUI(new Worker(),new Patient(),new Model());
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public AnswerUI(Worker w,Patient p,Model m) {
		//传入模板，找到模板中所有问题
		int len = modelService.getProblems(m).size();
		System.out.println(len);
		
		//自动生成
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 474, 491);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JButton btnNewButton = new JButton("结束答题");
		btnNewButton.setBounds(104, 407, 97, 23);
		contentPane.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("返回");
		btnNewButton_1.setBounds(254, 407, 97, 23);
		contentPane.add(btnNewButton_1);
		btnNewButton_1.addActionListener((e)->{
			new EvaluateUI(w, p).setVisible(true);
			setVisible(false);
			});
		
		JPanel panel = new JPanel();
		panel.setBounds(23, 10, 403, 387);
		contentPane.add(panel);
		panel.setLayout(new GridLayout(len*4, 1, 0, 10));
		
		//加滚动条
		JScrollPane jp = new JScrollPane(panel);
		jp.setBounds(23, 10, 403, 387);
		contentPane.add(jp);
		
		//用模板找到所有的问题
		ArrayList<Problem> problems = modelService.getProblems(m);
		this.group = new ButtonGroup[problems.size()];//一题一组
		//三种类型的按钮，计算分数用
		JRadioButton[] five = new JRadioButton[problems.size()];
		JRadioButton[] three = new JRadioButton[problems.size()];
		JRadioButton[] one = new JRadioButton[problems.size()];
		
		for(int i=0;i<problems.size();i++) {
			panel.add(new JLabel(problems.get(i).getProblem()));//加问题控件
			
			group[i] = new ButtonGroup();
			//加选项控件
			JRadioButton JRadioButton1 =new JRadioButton(problems.get(i).getOptionA());
			JRadioButton JRadioButton2 =new JRadioButton(problems.get(i).getOptionB());
			JRadioButton JRadioButton3 =new JRadioButton(problems.get(i).getOptionC());
			//按钮分类
			five[i] = JRadioButton1;
			three[i] = JRadioButton2;
			one[i] = JRadioButton3;
			//加入到面板
			panel.add(JRadioButton1);
			panel.add(JRadioButton2);
			panel.add(JRadioButton3);
			//加入按钮组
			group[i].add(JRadioButton1);
			group[i].add(JRadioButton2);
			group[i].add(JRadioButton3);
		}
		
		btnNewButton.addActionListener((e)->{//交卷，计算分数
			int x=0,y=0,z=0;
			for(int i=0;i<len;i++) {
				if(five[i].isSelected()) {
					x++;
				}else if(three[i].isSelected()) {
					y++;
				}else if(one[i].isSelected()) {
					z++;
				}
			}
			if(x+y+z <len) {
				JOptionPane.showMessageDialog(null, "您有未选题目，请作答完整");//提示全选
			}else {
				int score = 5*x+3*y+z;
				JOptionPane.showMessageDialog(null, "您的作答分数："+score);
				//加建议，service
				Result result = new Result();
				result.setDate(new Date().toLocaleString());
				result.setModelKind(m.getKind());
				result.setModelName(m.getName());
				result.setName(p.getName());
				result.setSex(p.getSex());
				result.setWorker(w.getName());
				//传w p result
				new SuggestionGive(w, p, result).setVisible(true);//跳转新窗口
				setVisible(false);
			}
		});
	}
}
